package com.ddshuai.easy;

import java.util.*;

/**
 * 描述 新通教育
 * <p>
 * 二进制手表顶部有 4 个 LED 代表小时（0-11），底部的 6 个 LED 代表分钟（0-59）。
 * <p>
 * 每个 LED 代表一个 0 或 1，最低位在右侧。
 * <p>
 * 例如，上面的二进制手表读取 “3:25”。
 * <p>
 * 给定一个非负整数 n 代表当前 LED 亮着的数量，返回所有可能的时间。
 * <p>
 * 输入: n = 1
 * 返回: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08", "0:16", "0:32"]
 * <p>
 * 输出的顺序没有要求。
 * 小时不会以零开头，比如 “01:00” 是不允许的，应为 “1:00”。
 * 分钟必须由两位数组成，可能会以零开头，比如 “10:2” 是无效的，应为 “10:02”。
 *
 * @author ddshuai
 * @date 2018-12-17 11:21
 **/
public class ReadBinaryWatch {
    public List<String> readBinaryWatch(int num) {
        List<String> list = new ArrayList<>(64);
        if (num < 10) {
            if (num == 0){
                list.add("0:00");
                return list;
            }
            for (int i = 0; i <= 4 && i <= num; i++) {
                //时钟的组合
                Set<String> hours = getCombination(4, i);
                //分钟
                Set<String> minutes = getCombination(6, num - i);
                if (hours.size() > 0) {
                    for (String hour : hours) {
                        int hs = Integer.parseInt(hour, 2);
                        if (hs < 12 && hs > 0) {
                            if (minutes.size() == 0 && num - i == 0){
                                if (!list.contains(String.valueOf(hs)+":00")) {
                                    list.add(String.valueOf(hs)+":00");
                                }
                            }else {
                                for (String minute : minutes) {
                                    int ms = Integer.parseInt(minute, 2);
                                    if (ms < 60) {
                                        if (!list.contains(String.valueOf(hs)+":" + (ms < 10 ? "0" + ms : ms))) {
                                            list.add(String.valueOf(hs)+":" + (ms < 10 ? "0" + ms : ms));
                                        }
                                    }
                                }
                            }
                        }
                    }
                } else {
                    if (i == 0){
                        for (String minute : minutes) {
                            int ms = Integer.parseInt(minute, 2);
                            if (ms < 60) {
                                if (!list.contains("0:" + (ms < 10 ? "0" + ms : ms))) {
                                    list.add("0:" + (ms < 10 ? "0" + ms : ms));
                                }
                            }
                        }
                    }

                }
            }
        }
        Collections.sort(list);
        return list;
    }

    public static Set<String> getCombination(int bits, int lights) {
        Set<String> strings = new HashSet<>(64);
        if (lights == 0) {
            return strings;
        }
        boolean reverse = false;
        if (lights > bits / 2) {
            lights = bits - lights;
            reverse = true;
        }
        for (int i = 0; i < bits; i++) {
            if (lights == 1) {
                if (bits == 4) {
                    if (reverse) {
                        char[] chars = "1111".toCharArray();
                        chars[i] = '0';
                        strings.add(String.valueOf(chars));
                    } else {
                        char[] chars = "0000".toCharArray();
                        chars[i] = '1';
                        strings.add(String.valueOf(chars));
                    }
                } else {
                    if (reverse) {
                        char[] chars = "111111".toCharArray();
                        chars[i] = '0';
                        strings.add(String.valueOf(chars));
                    } else {
                        char[] chars = "000000".toCharArray();
                        chars[i] = '1';
                        strings.add(String.valueOf(chars));
                    }
                }
            }
            if (lights == 2 || lights == 3) {
                for (int i1 = 0; i1 < bits; i1++) {
                    if (lights == 3) {
                        for (int i2 = 0; i2 < bits; i2++) {
                            if (bits == 4) {
                                if (reverse) {
                                    char[] chars = "1111".toCharArray();
                                    chars[i] = '0';
                                    chars[i1] = '0';
                                    chars[i2] = '0';
                                    if (i != i1 && i != i2 && i1 != i2) {
                                        strings.add(String.valueOf(chars));
                                    }

                                } else {
                                    char[] chars = "0000".toCharArray();
                                    chars[i] = '1';
                                    chars[i1] = '1';
                                    chars[i2] = '1';
                                    if (i != i1 && i != i2 && i1 != i2) {
                                        strings.add(String.valueOf(chars));
                                    }
                                }
                            } else {
                                if (reverse) {
                                    char[] chars = "111111".toCharArray();
                                    chars[i] = '0';
                                    chars[i1] = '0';
                                    chars[i2] = '0';
                                    if (i != i1 && i != i2 && i1 != i2) {
                                        strings.add(String.valueOf(chars));
                                    }
                                } else {
                                    char[] chars = "000000".toCharArray();
                                    chars[i] = '1';
                                    chars[i1] = '1';
                                    chars[i2] = '1';
                                    if (i != i1 && i != i2 && i1 != i2) {
                                        strings.add(String.valueOf(chars));
                                    }
                                }
                            }
                        }
                    } else {
                        if (bits == 4) {
                            if (reverse) {
                                char[] chars = "1111".toCharArray();
                                chars[i] = '0';
                                chars[i1] = '0';
                                if (i != i1) {
                                    strings.add(String.valueOf(chars));
                                }
                            } else {
                                char[] chars = "0000".toCharArray();
                                chars[i] = '1';
                                chars[i1] = '1';
                                if (i != i1) {
                                    strings.add(String.valueOf(chars));
                                }
                            }
                        } else {
                            if (reverse) {
                                char[] chars = "111111".toCharArray();
                                chars[i] = '0';
                                chars[i1] = '0';
                                if (i != i1) {
                                    strings.add(String.valueOf(chars));
                                }

                            } else {
                                char[] chars = "000000".toCharArray();
                                chars[i] = '1';
                                chars[i1] = '1';
                                if (i != i1) {
                                    strings.add(String.valueOf(chars));
                                }
                            }
                        }
                    }
                }
            }
        }

        return strings;
    }

    public static void main(String[] args) {
        System.out.println(new ReadBinaryWatch().readBinaryWatch(7));
    }
}
